package com.yesep.constant;

import com.yesep.handler.MessageHandler;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息相关的静态变量
 *
 * @author Administrator
 */
public class Constants {
    public final static String PACKAGE_HEADER_STR = "##";// 包头标识
    public final static int PACKAGE_CONTENT_LENGTH_STR = 4;// 内容包长度
    public final static char PACKAGE_TAILED_R_STR = '\r';
    public final static char PACKAGE_TAILED_N_STR = '\n';// <CR><LF>
    public final static String PACKAGE_EQUAL_SPLIT = ",";   //同项分隔符
    public final static String BASE_MESSAGE_SPLIT = ";";// 主体消息分隔符
    public final static String EXTEND_HEADER_TAILE_MESSAGE_SPLIT = "&&";// 扩展消息分隔符
    public final static String EXTEND_MESSAGE_SPLIT = ";";
    public final static String MESSAGE_KEY_VALUE_SPLIT = "=";// 值键对分隔符
    public final static String TRAN_MODEL_MESSAGE_EXPRESSION_SPLIT = ";";// 转换消息分隔符
    public final static String POLLUTION_FACTOR_EXPRESSION_SPLIT = "-";   //污染因子分隔符

    public final static String QN_EXPRESSION = "QN=[1-9]\\d{16}";// 请求编号QN=YYYYMMDDHHMMSSZZZ，用来唯一标识一个命令请求，用于请求命令或通知命令
    public final static String ST_EXPRESSION = "ST=\\d{2}";// ST系统编码表 21:地表水监测
    public final static String CN_EXPRESSION = "CN=\\d{4}";// 命令列表
    public final static String PW_EXPRESSION = "PW=[a-zA-z0-9]*";// 密码
    public final static String MN_EXPRESSION = "MN=[a-zA-z0-9]*";// 设备唯一标识 MN
    public final static String FLAG_EXPRESSION = "Flag=\\d{1}";// 是否拆分包及应答标志 Flag
    public final static String CP_EXPRESSION = "CP=&&\\S*&&";// 指令参数 CP
    public final static String PNUM_EXPRESSION = "PNUM=[0-9]*";// PNUM  总包数
    public final static String PNO_EXPRESSION = "PNO=[0-9]*";// PNO 当前包号
    public final static String PACKAGE_HEAD_EXPRESSION = "##\\d{4}"; //包头后面的四位校验,包括##  列如##1234
    public final static String DATATIME_EXPRESSION = "DataTime=[0-9]*";

    public final static int IS_RESPONSE_NO = 0;// 0-不应答
    public final static int IS_RESPONSE_YES = 1;// 1-应答
    public final static int PACKAGE_SEQUENCE_CONTAIN_FLAG = 0;// 2-数据包中不包含包序号和总包号两部分
    public final static int PACKAGE_SEQUENCE_NOT_CONTAIN_FLAG = 1;// 3-数据包中包含包序号和总包号两部分

    public final static Map<String, String> CMD_MESSAGE_TEMPLATE_MAP = new HashMap<String, String>();// 下行命令指定模板

    public static String LOCAL_IP = "";

//	public final static Map<String,EnvSiteCmd> UNFINISH_CMD_MESSAGE_MAP = new HashMap<String,EnvSiteCmd>();// 待完成的下行命令

    public final static int DATA_TEXT_TYPE_UP = 1;// 上行文
    public final static int DATA_TEXT_TYPE_DOWN = 0;// 下行文

    public final static String DATA_TEXT_STATUS_SUCCESS = "01";// 成功
    public final static String DATA_TEXT_STATUS_FAILED = "02";// 失败

    public final static String DATA_FACTOR_TYPE_NORMAL = "01";// 正常数据

    public final static String HEART_PACKAGE_CN = "1062";// 心跳包命令

    public final static String PACKAGE_TAILED_RN_STR = "\r\n";

    //响应报文
    public final static String UP_TO_DOWN_RESPONSE_CN = "9014";
    /**
     * 报文接口前缀、后缀
     */
    public final static String MESSAGE_INTERFACE_PREFIX = "Message";
    public final static String MESSAGE_INTERFACE_SUFFIX = "Service";

    static {
        CMD_MESSAGE_TEMPLATE_MAP.put("1012", "QN;ST;CN;PW;MN;Flag;CP{SystemTime}");// 设置现场机系统时间 QN=20160707010101001;ST=21;CN=1012;PW=123456;MN=581456A2100001;Flag=3;CP=&&SystemTime=20160707010101&&
        CMD_MESSAGE_TEMPLATE_MAP.put("1062", "QN;ST;CN;PW;MN;Flag;CP{RtdInterval}");// 设置实时数据（即心跳包）上报间隔 QN=20160707010101001;ST=21;CN=1062;PW=123456;MN=20112630451001;Flag=1;CP=&&RtdInterval=30&&
        CMD_MESSAGE_TEMPLATE_MAP.put("2051", "QN;ST;CN;PW;MN;Flag;CP{BeginTime,EndTime}");// 上传环境监测有效测量数据 QN=20160707010101001;ST=21;CN=2051;PW=123456;MN=88888880000001;Flag=3;CP=&&BeginTime=20160706000000,EndTime=20160706235959&&
        CMD_MESSAGE_TEMPLATE_MAP.put("3014", "QN;ST;CN;PW;MN;Flag;CP{PolId,CTime,CTime,CTime,CTime}");// 设置设备采样时间周期 QN=20160707010101001;ST=21;CN=3014;PW=123456;MN=20112630451001;Flag=1;CP=&&PolId=w01018,CTime=04,CTime=10,CTime=14,CTime=16&&
        CMD_MESSAGE_TEMPLATE_MAP.put("3015", "QN;ST;CN;PW;MN;Flag;CP{PolId,CMD}");// 远程反控下端设备 QN=20160707010101001;ST=21;CN=3015;PW=123456;MN=20112630451001;Flag=1;CP=&&PolId=w01018,CMD=110&&

        CMD_MESSAGE_TEMPLATE_MAP.put("9011", "QN;ST;CN;PW;MN;Flag;CP{QN,QnRtn}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9012", "QN;ST;CN;PW;MN;CP{QN,ExeRtn}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9013", "QN;ST;CN;PW;MN;Flag;CP{QN}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9014", "QN;ST;CN;PW;MN;Flag;CP{}");
//        CMD_MESSAGE_TEMPLATE_MAP.put("9014", "QN;ST;CN;PW;MN;Flag;CP{QN,QnRtn}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9015", "ST;CN;PW;MN;Flag;CP{QN,QnRtn}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9016", "ST;CN;PW;MN;Flag;CP{QN}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9017", "ST;CN;PW;MN;Flag;CP{QN}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9018", "ST;CN;PW;MN;Flag;CP{QN}");
        CMD_MESSAGE_TEMPLATE_MAP.put("9019", "ST;CN;PW;MN;Flag;CP{QN}");
        try {
            LOCAL_IP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    //public static Map<String, SiteModel> SITE_INFO_MAP = new HashMap<String, SiteModel>();// 站点缓存

    public static void main(String args[]) {
        String tempMessage = "QN=20160707010101001;ST=21;CN=2081;PW=123456;MN=001;Flag=1;CP=&&DataTime=20160707010101;d101-Rtd=27.01;d102-Rtd=65.1;d103-Rtd=12876.6;d104-Rtd=874.32;d105-Rtd=987;d111-Rtd=330;d112-Rtd=88;d113-Rtd=230;d114-Rtd=200;d115-Rtd=66;d116-Rtd=99&&";
        String packageMessage = MessageHandler.tranMessageToPackage(tempMessage);
        System.out.println(packageMessage);
//		MessageBaseModel messageBaseModel = MessageHandler.tranStringMessageBaseModel(packageMessage);
//		System.out.println(messageBaseModel.getErrorMessage());
//		packageMessage += packageMessage;
//		String[] sendMessageArray = packageMessage.split("\\r\\n");
//		for(String sendMessage:sendMessageArray){
//			System.out.println(sendMessage);
//		}
        //8.3上传环境监测实时数据（即心跳包）(CN=2011)心跳包
        //##0144QN=20160707010101001;ST=21;CN=2011;PW=123456;MN=001;Flag=1;CP=&&DataTime=20160707010101;B01-Rtd=100.3;w01001-Rtd=7.1;w01009-Rtd=9.2&&928a\r\n
        //请求响应
        //##0084ST=91;CN=9011;PW=123456;MN=581456A2100001;Flag=0;CP=&&QN=20160707010101001;QnRtn=1&&6dcf\r\n
        //返回操作执行结果
        //##0078ST=91;CN=9012;PW=123456;MN=581456A2100001;CP=&&QN=20160707010101001;ExeRtn=1&&a640\r\n
        //8.4上传环境监测设备运行状态(CN=2021)
        //##0138QN=20160707010101001;ST=21;CN=2021;PW=123456;MN=001;Flag=1;CP=&&DataTime=20160707010101;w000-Flag=9000;w01018-Flag=9000;w01019-Flag=9000&&9532\r\n
        //上传有效数据2051
        //##0119QN=20160707010101001;ST=21;CN=2051;PW=123456;MN=001;Flag=1;CP=&&DataTime=20160706000000;w01001-Rtd=7.1;w01003-Rtd=9.2&&b6ec
        //上传动环有效数据2081
        //##0239QN=20160707010101001;ST=21;CN=2081;PW=123456;MN=001;Flag=1;CP=&&DataTime=20160707010101;d101-Rtd=27.01;d102-Rtd=65.1;d103-Rtd=12876.6;d104-Rtd=874.32;d105-Rtd=987;d111-Rtd=330;d112-Rtd=88;d113-Rtd=230;d114-Rtd=200;d115-Rtd=66;d116-Rtd=99&&062a

//		String message = MessageHandler.getMessageFromPackage(PACKAGE_HEADER_STR+MessageHandler.paddingLength(String.valueOf(tempMessage.length()), 4, 0, "0")+tempMessage);
//		
//		System.out.println(MessageHandler.getStringForExpression(message, QN_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, ST_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, CN_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, PW_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, MN_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, FLAG_EXPRESSION));
//		System.out.println(MessageHandler.getStringForExpression(message, CP_EXPRESSION));
//		
//		// 转换请求报文格式内容
//		MessageBaseModel messageBaseModel =  MessageHandler.tranStringMessageBaseModel(message);
//		System.out.println("messageBaseModel["+messageBaseModel+"]");
//		
//		// 动态生成发送消息内容
//		String cmdMessage = MessageHandler.tranMessageModelToString(messageBaseModel, CMD_MESSAGE_TEMPLATE_MAP.get("3014"));
//		System.out.println("cmdMessage["+cmdMessage+"]");
//		
//		// 打包成命令发送
//		String packageMessage = MessageHandler.tranMessageToPackage(cmdMessage);
//		System.out.println("packageMessage["+packageMessage+"]");

//		System.out.println(MessageHandler.crcTable("QN=20081218105947818;ST=91;CN=9021;PW=123456;MN=81234567890001;Flag=1;CP=&&&&".getBytes()));
//		System.out.println(MessageHandler.crcTable("ST=32;CN=2011;QN=20071219113215000;PW=123456;MN=81234567890001;CP=&&&&".getBytes()));
//		System.out.println(MessageHandler.crcTable("ST=32;CN=2011;PW=123456;MN=81234567890001;CP=&&DataTime=20081218155521;B03-Rtd=-24.992,B03-Flag=N&&".getBytes()));

    }
}
